package com.ss.task.test;

import com.ss.task.model.TTaskEntity;
import com.ss.task.model.TaskEntity;
import com.ss.task.model.TaskUserEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.TaskService;
import com.ss.task.util.BaseController;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhongJian.
 */
@RestController
@RequestMapping("test/tTask/")
public class TTaskController extends BaseController {

    @Autowired
    TaskService taskService;

    @RequestMapping("doing")
    public ResultModel<List<TTaskEntity>> AndroidDoing(Integer offset, Integer length){
        System.out.println("yamiedie");
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllPubTasks(offset, length);
        return giveResultByTask(resultModel);
    }

    @RequestMapping("done")
    public ResultModel<List<TTaskEntity>> AndroidDoneList(Integer offset, Integer length){
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllFinishedTasks(offset, length);
        return giveResultByTask(resultModel);
    }

    @RequestMapping("over")
    public ResultModel<List<TTaskEntity>> AndroidOverList(Integer offset, Integer length){
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllEvaluateTasks(offset, length);
        return giveResultByTask(resultModel);
    }

    @RequestMapping("member/doing")
    public ResultModel<List<TTaskEntity>> AndroidMemberDoing (Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskUserEntity>> resultModel = taskService.getAllPubTasksByMember(user.getId(), offset, length);
        return giveResultByTaskUser(resultModel);
    }

    @RequestMapping("member/done")
    public ResultModel<List<TTaskEntity>> AndroidMemberDoneList (Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskUserEntity>> resultModel = taskService.getAllFinishedTasksByMember(user.getId(), offset, length);
        return giveResultByTaskUser(resultModel);
    }

    @RequestMapping("member/over")
    public ResultModel<List<TTaskEntity>> AndroidMemberOverList (Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskUserEntity>> resultModel = taskService.getAllEvaluateTasksByMember(user.getId(), offset, length);
        return giveResultByTaskUser(resultModel);
    }

    public ResultModel<List<TTaskEntity>> giveResultByTask(ResultModel<List<TaskEntity>> resultModel){
        List<TaskEntity> list = resultModel.getData();

        List<TTaskEntity> listTTask = new ArrayList<TTaskEntity>();

        for (int i=0;i<list.size();i++) {
            TTaskEntity tTaskEntity = new TTaskEntity();
            tTaskEntity.setName(list.get(i).getTitle());
            tTaskEntity.setType(list.get(i).getType().getTitle());
            tTaskEntity.setPublisher(list.get(i).getLeader().getRealname());
            tTaskEntity.setStart_time(list.get(i).getStartDate());
            tTaskEntity.setEnd_time(list.get(i).getEndDate());
            listTTask.add(tTaskEntity);
        }

        ResultModel<List<TTaskEntity>> resultModel1 = new ResultModel<List<TTaskEntity>>(listTTask);

        return resultModel1;
    }

    public ResultModel<List<TTaskEntity>> giveResultByTaskUser(ResultModel<List<TaskUserEntity>> resultModel){
        List<TaskUserEntity> list = resultModel.getData();
        List<TTaskEntity>  listTTask = new ArrayList<TTaskEntity>();
        for(int i=0;i<list.size();i++) {
            TTaskEntity tTaskEntity = new TTaskEntity();
            tTaskEntity.setName(list.get(i).getTask().getTitle());
            tTaskEntity.setType(list.get(i).getTask().getType().getTitle());
            tTaskEntity.setPublisher(list.get(i).getTask().getLeader().getRealname());
            tTaskEntity.setStart_time(list.get(i).getTask().getStartDate());
            tTaskEntity.setEnd_time(list.get(i).getTask().getEndDate());
            listTTask.add(tTaskEntity);
        }

        ResultModel<List<TTaskEntity>> resultModel1 = new ResultModel<List<TTaskEntity>>(listTTask);

        return resultModel1;
    }
}
